package com.ssd.controller;

import com.ssd.entity.SkuEntity;
import com.ssd.service.SkuService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sku")
@Slf4j
public class SkuController {

    @Resource
    private SkuService skuService;

    // 分页查询商品列表
    @GetMapping("/skuList")
    public BaseResponse<List<SkuEntity>> getSkuPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String skuName,
            @RequestParam(required = false) String innerCode) {
        return new BaseResponse(skuService.getSkuListWithClass(pageNum, pageSize, skuName,innerCode));
    }

    /**
     * 根据主键获取商品详情
     */
    @GetMapping("/{skuId}")
    public BaseResponse<SkuEntity> getSkuDetail(@PathVariable("skuId") Long skuId) {
        return new BaseResponse(skuService.selectSkuBySkuId(skuId));
    }

    /**
     * 新增商品信息
     */
    @PostMapping
    public BaseResponse<String> addSku(@RequestBody SkuEntity sku) {
        return new BaseResponse(skuService.insertSku(sku));
    }

    /**
     * 修改商品信息
     */
    @PutMapping
    public BaseResponse<String> updateSku(@RequestBody SkuEntity sku) {
        return new BaseResponse(skuService.updateSku(sku));
    }

    /**
     * 批量删除商品信息
     */
    @DeleteMapping("/{skuIds}")
    public BaseResponse<String> batchDeleteSku(@PathVariable("skuIds") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(skuService.deleteSkuBySkuIds(classIds));
    }
}